package Test.jsp.hasconn.containsecond;

import java.io.File;
import java.io.FileWriter;
import Test.tools.Utils;


public class SJView {

	public static void sJView(String tableName,String firstTable,String tableBean,String currentBao) throws  Exception{
		String rn = "\r\n";
		String []array = Utils.stringToArray(tableBean);
		StringBuilder sbname = new StringBuilder();
		for (int i = 0; i <  array.length && !(firstTable+"Id").equals(array[i]) ; i++) {
			String bean = 
				"	            <td class=\"td3\">" + rn + 
				"	              <font color=\"#0000FF\">"+array[i]+"</font>" + rn + 
				"	            </td>" + rn ;
			sbname.append(bean);
		}
		StringBuilder sbvalue = new StringBuilder();
		for (int i = 0; i <  array.length &&!(firstTable+"Id").equals(array[i]) ; i++) {
			String value = 
				"				<td width=\"20%\" class=\"td3\">" + rn + 
				"	              ${"+tableName+"List."+array[i]+"}" + rn + 
				"	            </td>" + rn ;
			sbvalue.append(value);
		}
		String src = 
		"<%@ page language=\"java\"  import=\"java.util.*\" pageEncoding=\"UTF-8\"%>" + rn + 
		"<%@ taglib uri=\"http://java.sun.com/jsp/jstl/core\" prefix=\"c\"%>" + rn + 
		"<%@ taglib prefix=\"form\" uri=\"http://www.springframework.org/tags/form\" %>" + rn + 
		"<%@ taglib prefix=\"fmt\" uri=\"http://java.sun.com/jsp/jstl/fmt\" %>" + rn + 
		"<%@ taglib prefix=\"fn\" uri=\"http://java.sun.com/jsp/jstl/functions\" %>" + rn + 
		"<%" + rn + 
		"String path = request.getContextPath();" + rn + 
		"String basePath = request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\";" + rn + 
		"%>" + rn + 

		"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">" + rn + 
		"<html>" + rn + 
		"  <head>" + rn + 
		"    <base href=\"<%=basePath%>\">" + rn + 
		    
		"    <title>"+tableName+"详细界面</title>" + rn + 
		"    <style type=\"text/css\">" + rn + 
		"    .td2 { border-bottom: #9999FF solid 1px; }" + rn + 
		"    .td3 { border-bottom: #9999FF solid 1px;  border-right: #9999FF solid 1px; }" + rn + 
		"    </style>" + rn + 
		"    <script language=\"javascript\">	" + rn + 
		"    function  del(obj){" + rn + 
		"    			var k = window.confirm(\"are you sure to delete this item?\");" + rn + 
		"      			if(k){" + rn + 
		"                     location.href=\""+tableName+"Del\"+obj+\".do\";" + rn + 
		"        			 return true;   " + rn +          
		"      	 		  }else{" + rn + 
		"         		 	return false;    " + rn +  
		"     			  }" + rn + 
		"     }" + rn + 
		"      function update(obj){" + rn + 
		"           location.href=\""+tableName+"GoUpdate\"+obj+\".do\";" + rn + 
		"      }" + rn + 
		"      function add(obj){" + rn + 
		"        location.href=\""+tableName+"GoAdd\"+obj+\".do\";" + rn + 
		"      }" + rn + 
		"      function goView(obj){" + rn + 
		"       location.href=\""+firstTable+"View\"+obj+\".do\";" + rn + 
		"      }" + rn + 
		"      function goback(){" + rn + 
		"       location.href=\""+firstTable+"View.do\";" + rn + 
		"      }" + rn + 
		"      </script>" + rn + 
		
		"  </head>" + rn + 
		"  <body>" + rn + 

		
		
		
		
		" <table border=\"1\" rules=\"none\" bordercolor=\"#969696\" style=\"empty-cells: show\"    " + rn + 
		" 	width=\"650\" cellpadding=\"2\" cellspacing=\"0\">                                     " + rn + 
		" 	<tr bgcolor=\"#CCFFFF\">                                                           " + rn +     
		" 		<td colspan=\"100\" class=\"td2\">                                               " + rn + 
		" 			<font color=\"0000FF\">"+tableName+"show</font>                                  " + rn + 
		" 		</td>                          " + rn + 
		" 	</tr>  " + rn + 
		
		"<tr><td colspan=\"100%\"><hr size=\"1\" color=\"#9999FF\"></hr></td></tr>           " + rn + 
		"<tr>           " + rn + 
		"	<td  bgcolor=\"#99CCFF\" class=\"td3\">           " + rn + 
		"		<font color=\"0000FF\">"+firstTable+"Id</font>           " + rn + 
		"	</td>           " + rn + 
		"	<td  bgcolor=\"#99CCFF\" class=\"td3\">           " + rn + 
		"			${"+tableName+"."+firstTable+"Id}           " + rn + 
		"	</td>           " + rn + 
		"</tr>           " + rn + 
	    "<tr><td colspan=\"100%\"><hr size=\"1\" color=\"#9999FF\"></hr></td></tr>           " + rn + 
		
		
		
		" 	<tr align=\"center\" bgcolor=\"#99CCFF\">            " + rn + 
		sbname + 		
		" 		<TD class=\"td2\">            " + rn + 
		" 			<font color=\"#0000FF\">operation</font>           " + rn +   
		" 		</TD>           " + rn + 
		" 	</tr>           " + rn + 
		" 	<c:forEach items=\"${"+tableName+"List}\" var=\""+tableName+"List\">           " + rn +  
		" 		<tr align=\"center\">           " + rn + 
		sbvalue +			
		" 			<td class=\"td2\">           " + rn +  
		" 				<a             " + rn + 
		" 					onclick=\"update(${"+tableName+"List."+array[0]+"});\">goupdate</a>           " + rn +  
		" 				<a           " + rn +  
		" 					onclick=\"del(${"+tableName+"List."+array[0]+"});\" >delete</a>           " + rn +    
		" 			</td>            " + rn +   
		" 		</tr>            " + rn + 
		" 	</c:forEach>            " + rn + 

		" 	<tr height=\"50\" align=\"right\">            " + rn + 
		" 		<td colspan=\"100%\" bgcolor=\"#CCFFFF\">            " + rn +  
		" 			<input type=\"button\"           " + rn +   
		" 				onclick=\"add(${"+tableName+"."+firstTable+"Id})\" value=\"goadd\"            " + rn +  
		" 				style=\"background: #CCFFFF\" />          " + rn + 
		" 			<input type=\"button\"           " + rn +   
		" 				onclick=\"goback()\" value=\"goback\"            " + rn +  
		" 				style=\"background: #CCFFFF\" />          " + rn + 
		" 		</td>            " + rn +   
		" 	</tr>           " + rn +   

		" </table>            " + rn +  
		
		
		
		
		
		
		"  </body>" + rn + 
		"</html>" + rn + 
		rn;
		
		
		String path = System.getProperty("user.dir")+"/WebRoot/back/jsp/"+tableName+"/";
		String fileName = System.getProperty("user.dir")+"/WebRoot/back/jsp/"+tableName+"/"+tableName+"View.jsp";
		File fpath = new File(path);
		if (!fpath.exists()) {
		   fpath.mkdirs();
	    }
		File f = new File(fileName);
		FileWriter fw = new FileWriter(f);
		fw.write(src);
		fw.flush();
		fw.close();
 }	
}
